<?php
namespace Menu\Controller;
use Think\Controller;
class AlbumController extends LoginController {
	protected $DB;
	public function __construct(){
		parent::__construct();
		$this->DB = D('Album');
	}
	public function add(){
		$this->display();
	}

	public function albumAdd(){
		$photo_cover = $this->upload();
		$data = $this->DB->AlbumAdd($photo_cover);
		if($data['number']){
			$this->success($data['info']);
		}
	}

	public function albumList(){
		$dataList = $this->DB->Read();
		$this->assign('List',$dataList['list']);
		$this->assign('page',$dataList['page']);
		$this->display();
	}

	public function getOne($album_id){
		layout(false);
		$dataList = $this->DB->oneList($album_id);
		$this->assign('List',$dataList);
		$this->display();
	}
	public function update($album_id){
		if(IS_POST){
			$photo_cover = $this->upload();
			$dataList = $this->DB->albumUpdate($album_id,$photo_cover);
			if($dataList['number']){
				$this->success($dataList['info']);
			}
		}
		$dataList = $this->DB->oneList($album_id);
		$this->assign('List',$dataList);
		$this->assign('album_id',$album_id);
		$this->display();
	}

	public function del($album_id){
		$msg = $this->DB->del($album_id);
		echo $msg;
	}

	public function upload(){
		$upload = new \Think\Upload();// 实例化上传类
		$upload->maxSize   =     3145728 ;// 设置附件上传大小
		$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		$upload->rootPath  =      './Public'; // 设置附件上传根目录
		$upload->savePath  =      './Uploads/'; // 设置附件上传（子）目录
		//上传单个文件 
		$info   =   $upload->uploadOne($_FILES['album_cover']);
		if(!$info) {
			// 上传错误提示错误信息
			$this->error($upload->getError());
		}else{
			//上传成功 获取上传文件信息
			$photo_cover =  '/Public'.ltrim($info['savepath'].$info['savename'],'.');
			return $photo_cover;
		}
	}


	public function file($album_id){
		$this->assign('album_id',$album_id);
		$this->display();
	}

}